#!/bin/bash

PAR="$1"
CMD="$(which $PAR)"
DB=/var/cache/packages/slackware-libs.txt

if [ "$CMD" = "" ]; then
   echo "Command $PAR not found"
   exit 1
fi

echo ""
echo "===="
echo "Missing libraries can be found in the following Slackware packages:"
echo ""

DEPS="$(ldd "$CMD" 2>/dev/null | egrep '^\s' | fgrep 'not found' | sed -r 's/ =>.*//' | sed -r 's@^/@@')"

echo ""
echo "$DEPS" | while read LIBRARY; do
   LIBRARY="$(echo "$LIBRARY" | sed -r 's/[][\.|$(){}?+*^]/\\&/g')"
   grep "^$LIBRARY:" $DB
done
echo ""

echo "----"
echo "You should be able to fix missing dependencies with the following command:"
echo ""
echo -ne "slackpkg install"

echo "$DEPS" | while read LIBRARY; do
   LIBRARY="$(echo "$LIBRARY" | sed -r 's/[][\.|$(){}?+*^]/\\&/g')"
   grep "^$LIBRARY:" $DB
done | cut -d ":" -f 2- | sed -r "s/,.*//" | sort | uniq
